home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / OVSORT.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  5KB  |  183 lines

  1. /*  012  11-Oct-86  ovsort.c
  2.  
  3.         Sort routines for OVERVIEW.
  4.  
  5.         Copyright (c) 1986 by Blue Sky Software.  All rights reserved.
  6. */
  7.  
  8. #include "ov.h"
  9.  
  10. #ifndef NULL
  11. #define NULL (0)
  12. #endif
  13.  
  14. extern struct window cw;
  15. extern struct file_ent files[];
  16.  
  17. static char *nullname = "";
  18.  
  19. int sortname(), sortext(), sortdate(), sortsize(), sortidx();
  20.  
  21. char *strchr();
  22.  
  23.  
  24. /******************************************************************************
  25.  **                             S O R T                                      **
  26.  *****************************************************************************/
  27.  
  28. sort_asc() {           /* sort files in ascending order */
  29.    cw.sortopt = 'A';
  30.    sort_and_disp(NULL);
  31. }
  32.  
  33. sort_desc() {          /* sort files in descending order */
  34.    cw.sortopt = 'D';
  35.    sort_and_disp(NULL);
  36. }
  37.  
  38. sort_none() {          /* do not sort files (dos dir order) */
  39.    sort_and_disp(sortidx);
  40. }
  41.  
  42.  
  43. sort_name() {          /* sort files by name */
  44.    sort_and_disp(sortname);
  45. }
  46.  
  47. sort_ext() {           /* sort files by extension */
  48.    sort_and_disp(sortext);
  49. }
  50.  
  51. sort_date() {          /* sort files by date */
  52.    sort_and_disp(sortdate);
  53. }
  54.  
  55. sort_size() {          /* sort files by size */
  56.    sort_and_disp(sortsize);
  57. }
  58.  
  59.  
  60. /*****************************************************************************
  61.                          S O R T _ A N D _ D I S P
  62.  *****************************************************************************/
  63.  
  64. sort_and_disp(sortrtn) /* sort files and update window */
  65. int (*sortrtn)();
  66. {
  67.    char fname[MAX_NAMELEN+1];
  68.    register struct file_ent *fp;
  69.  
  70.    strcpy(fname,files[cw.curidx].name); /* save current file name */
  71.  
  72.    sort_files(sortrtn);                /* sort files in desired order */
  73.  
  74.    /* after doing a sort, curidx no longer points to the same file.
  75.       Search files[] for the old file name and move current pointer there. */
  76.  
  77.    for (cw.curidx = 0, fp = files; cw.curidx < cw.nfiles; fp++, cw.curidx++)
  78.        if (strcmp(fp->name,fname) == 0)
  79.           break;
  80.  
  81.    adjust_window();                    /* update window display */
  82.    update_window(1);
  83. }
  84.  
  85.  
  86. /******************************************************************************
  87.  **                         S O R T _ F I L E S                              **
  88.  *****************************************************************************/
  89.  
  90. sort_files(sortrtn)    /* sort the file info by the user selected method */
  91. int (*sortrtn)();
  92. {
  93.    if (sortrtn)                /* if caller specified a sort routine, */
  94.       cw.sortfunc = sortrtn;   /*   make it the new default */
  95.  
  96.    qsort(files,cw.nfiles,sizeof(struct file_ent),cw.sortfunc);
  97.  
  98. }
  99.  
  100.  
  101. #define sortorder(x) ((cw.sortopt == 'A') ? x : -x)
  102.  
  103. sortname(f1,f2)        /* support function to sort by name */
  104. struct file_ent *f1, *f2;
  105. {
  106.    return(sortorder(strcmp(f1->name,f2->name)));
  107. }
  108.  
  109. static int
  110. sortsize(f1,f2)        /* support function to sort by size */
  111. register struct file_ent *f1, *f2;
  112. {
  113.    int rc;
  114.  
  115.    if (f1->size > f2->size)
  116.       rc = 1;
  117.    else
  118.       if (f1->size < f2->size)
  119.          rc = -1;
  120.       else
  121.          rc = strcmp(f1->name,f2->name);       /* sort by name if same size */
  122.  
  123.    return(sortorder(rc));
  124. }
  125.  
  126. static int
  127. sortext(f1,f2)         /* support function to sort by extension */
  128. struct file_ent *f1, *f2;
  129. {
  130.    int rc;
  131.    register char *ep1, *ep2;
  132.  
  133.    ep1 = strchr(f1->name,'.');         /* find the extension for f1 */
  134.    if (ep1 == NULL)
  135.       ep1 = nullname;
  136.    else
  137.       ep1++;
  138.  
  139.    ep2 = strchr(f2->name,'.');         /* find the extension for f2 */
  140.    if (ep2 == NULL)
  141.       ep2 = nullname;
  142.    else
  143.       ep2++;
  144.  
  145.    if ((rc = strcmp(ep1,ep2)) == 0)    /* sort by whole name if ext's equal */
  146.       rc = strcmp(f1->name,f2->name);
  147.  
  148.       return(sortorder(rc));
  149. }
  150.  
  151. static int
  152. sortdate(f1,f2)        /* support function to sort by date/time */
  153. register struct file_ent *f1, *f2;
  154. {
  155.    int rc;
  156.  
  157.    if (f1->date > f2->date)
  158.       rc = 1;
  159.    else
  160.       if (f1->date < f2->date)
  161.          rc = -1;
  162.       else
  163.          if (f1->time > f2->time)
  164.             rc = 1;
  165.          else
  166.             if (f1->time < f2->time)
  167.                rc = -1;
  168.             else
  169.                rc = strcmp(f1->name,f2->name);
  170.  
  171.    return(sortorder(rc));
  172. }
  173.  
  174. static int
  175. sortidx(f1,f2)         /* sort by index # (dos order) */
  176. struct file_ent *f1, *f2;
  177. {
  178.    if (f1->index > f2->index)
  179.      return(1);
  180.    else
  181.      return(-1);
  182. }
  183.